/*
 * Copyright (c) 2010, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ALKES_WIN32_EVENT_DRIVER_H_INCLUDED_
#define ALKES_WIN32_EVENT_DRIVER_H_INCLUDED_

#include "alkes.h"
#include "alkes/win32app/internal.h"
#include "alkes/win32app/Win32Window.h"
#include "alkes/win32app/Win32Event.h"

namespace alkes {

template<class T, class M>
class Win32EventDriver
    : public IEventDriver
{
public:
    Win32EventDriver();
    virtual ~Win32EventDriver();

    bool initialize(uint16_t width, uint16_t height, const char* title);
    void finalize();

    virtual int mainloop(IEventDispatcher* dispatcher);

    HWND getHWND();
    typename T::GraphicsType* getGraphics();
    typename T::ScreenType* getScreen();

private:
    Win32Window window_;
    intrusive_ptr<typename T::GraphicsType> graphics_;
    typename T::ScreenType* screen_;
    IEventDispatcher* dispatcher_;
};

template<class T, class M>
Win32EventDriver<T, M>::Win32EventDriver()
: graphics_(0)
, screen_(0)
, dispatcher_(0)
{
}

template<class T, class M>
Win32EventDriver<T, M>::~Win32EventDriver()
{
    graphics_ = 0;
    window_.finalize();
}

template<class T, class M>
HWND Win32EventDriver<T, M>::getHWND()
{
    return window_.getHWND();
}

template<class T, class M>
typename T::GraphicsType* Win32EventDriver<T, M>::getGraphics()
{
    return graphics_;
}

template<class T, class M>
typename T::ScreenType* Win32EventDriver<T, M>::getScreen()
{
    return screen_;
}

template<class T, class M>
bool Win32EventDriver<T, M>::initialize(uint16_t width, uint16_t height, const char* title)
{
    bool ret = false;
    if (window_.initialize(width, height, title))
    {
        graphics_ = T::createGraphics(&window_);

        if (graphics_)
            screen_ = new T::ScreenType(graphics_);

        if (screen_)
        {
            window_.setUser(graphics_);
            ret = true;
        }
        else
        {
            graphics_ = 0;
            window_.finalize();
        }
    }
    return ret;
}

template<class T, class M>
void Win32EventDriver<T, M>::finalize()
{
    delete screen_;
    screen_ = 0;
    graphics_ = 0;
}

template<class T, class M>
int Win32EventDriver<T, M>::mainloop(IEventDispatcher* dispatcher)
{
    window_.show(Win32Window::AL_WINDOW_SHOW);

    return M::mainloop(window_.getHWND(), screen_, dispatcher);
}

}

#endif
